<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * Record
 * @package	Codeigniter
 * @author	Chhing Chhing
 * @link	abc@gmail.com
 */

class Record extends PNC_Controller {
	/*
	 * Constructor
	 */

	public function __construct() {
		parent::__construct();
		$this->load->model(array('mod_record', 'mod_option', 'mod_employee', 'mod_leave_request'));
	}

	/*
	 * Index
	 * @access public
	 * @return void
	 */

	public function index() {
		redirect('record/show_record/');
	}

	/*
	 * Show Record
	 * @access public
	 * @return void
	 */

	public function show_record() {
		if ($this->check_session()) {
			$this->session->set_userdata("cur_page", $this->uri->segment(3));
			$this->session->set_userdata("pagination", $this->uri->segment(4));
			$data['title'] = 'Record';
			$config['total_rows'] = PNC_Model::count_padding_approval(0);
			$this->session->set_userdata('num_padding', $config['total_rows']);
			$num_per_page = 10;
			/* in case user delete query string */
			if ($this->session->userdata('per_page')) {
				$this->session->unset_userdata('per_page');
			}
			/* get limit per page from query string */
			if ($this->input->get('limit', TRUE)) {
				$this->session->set_userdata('per_page', $this->input->get('limit', TRUE));
				$num_per_page = $this->session->userdata('per_page');
			}
			$config['total_rows'] = ''; // initialize total records
			$config['base_url'] = site_url("record/show_record/annual/"); // initialize base url
			if ($this->uri->segment(3) === 'annual' OR !$this->uri->segment(3)) {
				$config['base_url'] = site_url("record/show_record/annual/");
				$config['total_rows'] = PNC_Model::count_data('lea_id', 1, 'pnc_record'); //get from Super class PNC_Model
			} else if ($this->uri->segment(3) === 'catchup') {
				$config['base_url'] = site_url("record/show_record/catchup/");
				$config['total_rows'] = PNC_Model::count_data('lea_id', 2, 'pnc_record');
			} else if ($this->uri->segment(3) === 'padding') {
				$config['base_url'] = site_url("record/show_record/pending/");
				$config['total_rows'] = PNC_Model::count_padding_approval(0);
			} else if ($this->uri->segment(3) === 'approval') {
				$config['base_url'] = site_url("record/show_record/approval/");
				$config['total_rows'] = PNC_Model::count_padding_approval(1);
			}
			$config['per_page'] = $num_per_page;
			$config['uri_segment'] = 4;
			$choice = $config["total_rows"] / $config["per_page"];
			$config["num_links"] = round($choice);
			$config['first_link'] = 'First';
			$config['next_link'] = 'Next';
			$config['prev_link'] = 'Pre';
			$config['last_link'] = 'Last';

			$this->pagination->initialize($config); //function to show all pages
			$page = ($this->uri->segment($config['uri_segment']) && $this->uri->segment($config['uri_segment']) > 0) ? $this->uri->segment($config['uri_segment']) : 0;
			$data['leave_record'] = ''; // initalize record seleted
			if ($this->uri->segment(3) === 'annual' OR !$this->uri->segment(3)) {
				/* get leave record as annual */
				$data['leave_record'] = $this->mod_record->show_record($config['per_page'], $page);
			} else if ($this->uri->segment(3) === 'catchup') {
				/* get leave record as catchup */
				$data['leave_record'] = $this->mod_record->show_catchup($config['per_page'], $page);
			} else if ($this->uri->segment(3) === 'pending') {
				/* get leave recrod as padding */
				$data['leave_record'] = $this->mod_record->padding_approved($config['per_page'], $page, 'pending');
			} else if ($this->uri->segment(3) === 'approval') {
				/* get leave record as approval */
				$data['leave_record'] = $this->mod_record->padding_approved($config['per_page'], $page, 'approval');
			}

			$data['pagination'] = $this->pagination->create_links();
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/*
	 * Edit Record
	 * @access public
	 * @return void
	 */

	public function edit_record() {
		if ($this->check_session()) {
			$this->session->set_userdata('old_lea_id', $this->uri->segment(3));
			$this->session->set_userdata('old_desc_id', $this->uri->segment(5));
			$this->session->set_userdata('rcd_id', $this->uri->segment(4));
			if ($this->uri->segment(3) == 1) {
				$data['edit_annual'] = PNC_Model::show_detail($this->uri->segment(4), 'annual');
			} else if ($this->uri->segment(3) == 2) {
				$data['edit_catchup'] = PNC_Model::show_detail($this->uri->segment(4), 'catchup');
			}
			$data['title'] = 'Edit record';
			$data['hour'] = array('' => '-- Select --', '4' => '4 Hours', '8' => '8 Hours', '12' => '12 Hours', '16' => '16 Hours');
			$data['rcd_status'] = array('' => '-- Select --', '0' => 'Disapprove', '1' => 'Approve');
			$data['leave_type'] = $this->mod_option->get_leaving_type();
			$data['sub_type'] = $this->mod_option->get_sub_leaving();
			$data['validator'] = $this->mod_option->get_validator();
			$data['ssl_type'] = $this->mod_option->get_ssl_type();
			if ($this->input->post('btn_update')) {
				$old_lea_id = $this->input->post('old_lea_id');
				$old_desc_id = $this->input->post('old_desc_id');
				$record_id = $this->input->post('record_id');
				/* Old post from form update */
				$old_sub_type = $this->input->post('old_sub_type');
				$old_ss_type = $this->input->post('old_ss_type');
				$old_reason = $this->input->post('old_reason');
				$old_start_date = $this->input->post('old_s_date');
				$old_end_date = $this->input->post('old_e_date');
				$old_desc_up = $this->input->post('old_desc_up');
				$old_working_on = $this->input->post('old_working_on');
				$old_catchup_on = $this->input->post('old_catchup_on');
				$old_hour = $this->input->post('old_duration');
				/* Check and Compare value */
				$supper_type = !$this->input->post('super_type') ? $old_lea_id : $this->input->post('super_type');
				$sub_type = !$this->input->post('sub_type') ? $old_sub_type : $this->input->post('sub_type');
				$ss_type = !$this->input->post('ss_type') ? $old_ss_type : $this->input->post('ss_type');
				$reason = !$this->input->post('txt_reason') ? $old_reason : $this->input->post('txt_reason');
				$start_date = !$this->input->post('s_date') ? $old_start_date : $this->input->post('s_date');
				$end_date = !$this->input->post('e_date') ? $old_end_date : $this->input->post('e_date');
				$desc_up = !$this->input->post('desc_up') ? $old_desc_up : $this->input->post('desc_up');
				$working_on = !$this->input->post('working_on') ? $old_working_on : $this->input->post('working_on');
				$catchup_on = !$this->input->post('catchup_on') ? $old_catchup_on : $this->input->post('catchup_on');
				$hour = !$this->input->post('duration') ? $old_hour : $this->input->post('duration');
				$days = ($hour / 8);
				$result = '';
				/* If update with change leave type */
				if ($supper_type != $old_lea_id) {
					if ($supper_type == 1) {
						PNC_Model::delete_datas('desc_id', $old_desc_id, '', 'pnc_cup_des'); //($where, $id, $lea_type, $table)
						$result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
					} else if ($supper_type == 2) {
						$result = PNC_Model::update_catchup_before($record_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
					}
				} else {
					if ($supper_type == 1) {
						$result = PNC_Model::update_annual_before($record_id, $supper_type, $sub_type, $ss_type, $reason, $start_date, $end_date, $hour, $days);
					} else if ($supper_type == 2) {
						$result = PNC_Model::update_catchup($record_id, $old_desc_id, $supper_type, $reason, $start_date, $end_date, $hour, $days, $desc_up, $working_on, $catchup_on);
					}
				}
				if ($result > 0) {
					$this->session->set_userdata('edit_success', show_message('Edited successfully!', 'success'));
					if ($this->session->userdata('per_page')) {
						redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
					}
					redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
				} else {
					$this->session->set_userdata('edit_erro', show_message('Editing was fail!', 'error'));
					redirect('record/edit_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
				}
			} else {
				$this->view_main($data);
			}
		} else {
			redirect('main/');
		}
	}

	/* Add new record for staff */

	public function add_record() {
		if ($this->check_session()) {
			$data['title'] = 'Add New Record';
			$data['opt_staff'] = $this->mod_option->get_staff_name();
			$data['super'] = $this->mod_leave_request->super_type_leave();
			$data['type'] = $this->mod_leave_request->type_leave();
			$data['sub_type'] = $this->mod_leave_request->sub_type_leave();
			$data['sub_type_per'] = $this->mod_leave_request->sub_type_leave_per();
			if ($this->input->post('btn_add_rcd')) {
				$config = array(
					array(
						'field' => 'duration',
						'label' => 'Time Duration',
						'rules' => 'trim|required|xss_clean'
					),
					array(
						'field' => 'staff_name',
						'label' => 'Staff\'s Name',
						'rules' => 'trim|required|greater_than[0]'
					),
					array(
						'field' => 'reason',
						'label' => 'Reason',
						'rules' => 'trim|alpha_string|required|xss_clean'
					),
					array(
						'field' => 'super_type',
						'label' => 'Type of Leave',
						'rules' => 'trim|required|greater_than[0]'
					),
					array(
						'field' => 's_date',
						'label' => ' Start Date',
						'rules' => 'trim|required|callback_compareDates'
					),
					array(
						'field' => 'e_date',
						'label' => 'End Date',
						'rules' => 'trim|required|callback_compareDates'
					)
				);
				$this->form_validation->set_rules($config);
				$this->form_validation->set_message('alpha_string', 'Please check what you fill.');
				$this->form_validation->set_message('greater_than[0]', 'The field must be select.');
				if ($this->form_validation->run() === FALSE) {
					$this->session->set_userdata('validate_fail', show_message('Please check your completed form!', 'error'));
					$this->view_main($data);
				} else {
					$staff = $this->input->post('staff_name');
					$validator = $this->session->userdata('staff_id');
					$super_type = $this->input->post('super_type');
					$sub_type = $this->input->post('sub');
					$sub_sub = $this->input->post('sub_sub');
					$desc = $this->input->post('desc_up');
					$work_on = $this->input->post('working_on');
					$catch_on = $this->input->post('catchup_on');
					$reason = $this->input->post('reason');
					$start_date = $this->input->post('s_date');
					$end_date = $this->input->post('e_date');
					$hour = $this->input->post('duration');
					$day = ($hour / 8);

					if ($start_date <= $end_date) {
						$success_request = '';
						if ($super_type == 1) {
							$success_request = PNC_Model::add_record_staff($staff, $validator, $super_type, $sub_type, $sub_sub, '', $reason, $start_date, $end_date, $hour, $day, '', '');
							if ($success_request > 0) {
								$this->session->set_userdata('add_seccuss', show_message('You have been added record successfully!', 'success'));
								redirect('record/show_record');
							} else {
								$this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
								$this->view_main($data);
							}
						} else if ($super_type == 2) {
							$success_request = PNC_Model::add_record_staff($staff, $validator, $super_type, '', '', $desc, $reason, $start_date, $end_date, $hour, $day, $work_on, $catch_on);
							if ($success_request > 0) {
								$this->session->set_userdata('add_seccuss', show_message('You have been added record successfully!', 'success'));
								redirect('record/show_record');
							} else {
								$this->session->set_userdata('sending_erro', show_message('Can not do request leaving!', 'error'));
								$this->view_main($data);
							}
						}
					} else {
						$this->session->set_userdata('sent_erro', show_message('Start date must be smaller than end date!', 'error'));
						$this->view_main($data);
					}
				}
			} else {
				$this->view_main($data);
			}
		} else {
			redirect('main/');
		}
	}

	/*
	 * Delete multi records
	 * @access public
	 * Call function delete_checked() from PNC_Controller.php
	 */

	public function delete_records() {
		if ($this->check_session()) {
			$checked_id = $this->input->post('rcdChecked');
			/* PNC_Controller::delete_checked($controller, $function, $model, $checked_id, $lea_type, $where, $table) */
			PNC_Controller::delete_checked('record', 'show_record', 'PNC_Model', $checked_id, 'annual', 'rcd_id', 'pnc_record');
		} else {
			redirect('main/');
		}
	}

	public function delete_records_catchup() {
		if ($this->check_session()) {
			$checked_id = $this->input->post('rcdChecked');
			PNC_Controller::delete_checked('record', 'show_record', 'PNC_Model', $checked_id, 'catchup', 'rcd_id', 'pnc_record');
		} else {
			redirect('main/');
		}
	}

	public function annual() {
		if ($this->check_session()) {
			redirect('record/show_record/annual/');
		} else {
			redirect('main/');
		}
	}

	public function catchup() {
		if ($this->check_session()) {
			redirect("record/show_record/catchup/");
		} else {
			redirect('main/');
		}
	}

	public function delete() {
		if ($this->check_session()) {
			$this->session->set_userdata('get_id', $this->uri->segment(3));
			$page = $this->session->userdata('get_page');
			$id = $this->session->userdata('get_id');
			$id_staff = $this->uri->segment(4);
			$data['get_name_staff'] = PNC_Model::get_name_staff($id_staff);
			$full_name = '';
			foreach ($data['get_name_staff']->result() as $row) {
				$full_name = strtoupper($row->emp_lname) . ' ' . ucfirst($row->emp_fname);
			}
			$result = $this->mod_record->delete_once($id, 'rcd_id', 'pnc_record', '');
			$sms = $full_name . '\'s leaving request has been deleted successfully!';
			$this->session->set_userdata('delete_success', show_message($sms, 'success'));
			if ($this->session->userdata('per_page')) {
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
			}
			redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
		} else {
			redirect('main/');
		}
	}

	public function deleting() {
		$pages = $this->session->userdata('page');
		$ts = $this->uri->total_segments(); /* Get last segment of uri */
		$desc_id = $this->uri->segment($ts - 1);
		$id = $this->uri->segment($ts - 2);
		$id_staff = $this->uri->segment($ts);
		$data['get_name_staff'] = PNC_Model::get_name_staff($id_staff);
		foreach ($data['get_name_staff']->result() as $row) {
			$full_name = strtoupper($row->emp_lname) . ' ' . ucfirst($row->emp_fname);
		}

		$result = $this->mod_record->delete_once($id, 'rcd_id', 'pnc_record', $desc_id);
		$sms = $full_name . '\'s leaving request has been deleted successfully!';
		$sms_error = $full_name . '\'s leaving request can not delete';
		if ($result) {
			$this->session->set_userdata('delete_success', show_message($sms, 'success'));
			if ($this->session->userdata('per_page')) {
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
			}
			redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
		} else {
			$this->session->set_userdata('delete_error', show_message($sms_error, 'success'));
			if ($this->session->userdata('per_page')) {
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
			}
			redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
		}
	}

	/*
	 * Approve for requesting
	 * Send mail to employee
	 */

	public function approved() {
		$id = $this->uri->segment(4);
		$current_status = $this->uri->segment(3);
		$id_staff = $this->uri->segment(5);
		$result = $this->mod_record->approve_update($id, $current_status, 'rcd_status', 'pnc_record');
		$data['get_name_staff'] = PNC_Model::get_name_staff($id_staff);
		foreach ($data['get_name_staff']->result() as $row) {
			$full_name = strtoupper($row->emp_lname) . ' ' . ucfirst($row->emp_fname);
		}
		if ($result > 0) {
			$sms_approve = $full_name . '\'s leaving request has been approved!';
			$sms_disapprove = $full_name . '\'s leaving request has been disapproved!';
			if ($current_status == '0') {
				$this->session->set_userdata('approve_success', show_message($sms_approve, 'success'));
				if ($this->session->userdata('per_page')) {
					redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			} else {
				$this->session->set_userdata('disapprove_success', show_message($sms_disapprove, 'success'));
				if ($this->session->userdata('per_page')) {
					redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			}
		} else {
			if ($current_status == '0') {
				$this->session->set_userdata('approve_error', show_message($sms_approve, 'error'));
				if ($this->session->userdata('per_page')) {
					redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			} else {
				$this->session->set_userdata('disapprove_error', show_message($sms_disapprove, 'error'));
				if ($this->session->userdata('per_page')) {
					redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination') . '?limit=' . $this->session->userdata('per_page'));
				}
				redirect('record/show_record/' . $this->session->userdata('cur_page') . '/' . $this->session->userdata('pagination'));
			}
		}
	}

	/* Get Profile of staff */

	public function get_profile() {
		if ($this->check_session()) {
			$data['title'] = 'Profile';
			$data['get_profile'] = PNC_Model::get_profile($this->session->userdata('staff_id'));
			$this->view_main($data);
		} else {
			redirect('main/');
		}
	}

	/* Change new password */

	public function change_password() {
		if ($this->check_session()) {
			if ($this->input->post('btn_save_change')) {
				$old_pass = $this->input->post('old_pass');
				$new_pass = $this->input->post('new_pass');
				$con_pass = $this->input->post('con_pass');
				$old_pass = md5($old_pass);
				$pass = $this->session->userdata('old_pass');

				if ($old_pass === '' OR $new_pass === '' OR $con_pass === '') {
					$this->session->set_userdata('input_blank', show_message('Please input all fields!', 'error'));
					redirect('record/change_pass');
				} else if (strlen($new_pass) < 5 OR strlen($new_pass) > 15) {
					$this->session->set_userdata('min_length', show_message('Password must be longer than 5 and shorter than 15 characters!', 'error'));
					redirect('record/change_pass');
				} else if ($old_pass != $pass) {
					$this->session->set_userdata('opass_err', show_message('Your old password is not match!', 'error'));
					redirect('record/change_pass');
				} else if ($new_pass != $con_pass) {
					$this->session->set_userdata('npass_err', show_message('New password and confirm password is not match!', 'error'));
					redirect('record/change_pass');
				} else {
					$new_pass = md5($new_pass);
					PNC_Model::change_password($old_pass, $new_pass, $con_pass, $this->session->userdata('staff_id'));
					$this->session->set_userdata('change_success', show_message('Changed password successfully!', 'success'));
					redirect('record/get_profile');
				}
			} /* End of press button update password */
		} else {
			redirect('main/');
		}
	}

	public function change_pass() {
		$data['title'] = 'Change Password';
		if ($this->input->post('btn_save_change')) {
			$this->change_password();
			redirect('record/get_profile');
		}
		$this->view_main($data);
	}

}

/* End of file record.php */

/* Location: ./application/models/record.php */